#define JET2RGB_IMPLEMENTATION
#include "level1.h"
#include "raylib.h"
#include "imgui.h"
#include "rlImGui.h"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <emscripten/emscripten.h>
#include <pthread.h>
#include <complex>

static bool ready = false;
static void InitLevel1();
static void FEXDraw(void);
static pthread_t ptt;
static int x=0,y=0,px=0,py=0,n=30,in1=0,in2=0,nn=2,posx=0,posy=0,nposx=0,nposy=0;
static bool one_window = true ,two_window = true ,draw = false,ok = true;
static float r=0.11,i=0.66;
static ImDrawList *wdl = nullptr;
static ImVec2 imv2;
static Color coll={0,0,0,0};
static char buff[64];
static Image Bimg;
static Texture2D Btex;

std::complex<float> fc[30] = {0};
// int mx[4000][4000] = {0};
typedef struct {
    int x,y;
}iVector2;

//复平面坐标转换屏幕坐标
iVector2 real_to_mpos(std::complex<float> val){
    float x = val.real();
    float y = val.imag();
    int px = (x-(-2.0))/0.001;
    int py = (y-(-2.0))/0.001;
    return (iVector2){int(px),int(py)};
}

void SetLevel1_ready(bool _ready){
    ready = _ready;
}

void UpdateLevel1(){

    if (!ready){
        InitLevel1();
        ready = true;
    }
    x = emscripten_run_script_int("window.innerWidth");
    y = emscripten_run_script_int("window.innerHeight");
    if (x!=px || y!=py){
            px=x;
            py=y;
    }
    
    BeginDrawing();
    rlImGuiBegin();

    if(!draw){
        DrawTextureRec(Btex,(Rectangle){0,0,(float)x,(float)y} , (Vector2){0,0}, WHITE);
    }else{
        ClearBackground((Color){0,0,128,255});
    }

    if(2){
        ImGui::Begin("input",&two_window);
        ImGui::SetWindowSize({200,140});
        posx = ImGui::GetWindowPos().x;
        posy = ImGui::GetWindowPos().y;
        if(posx != nposx || posy != nposy){
            nposx = posx;
            nposy = posy;
        }
        ImGui::InputFloat("real",&r,0.001,0.001);
        ImGui::InputFloat("image",&i,0.001,0.001);
        ImGui::InputInt("n",&nn);
        if(ImGui::Button("Draw") && !draw){
            draw = true;
            std::printf("start!!\n");
            FEXDraw();
            std::printf("ok!!\n");
            draw = false;
        }
        ImGui::LabelText("","x:%d,y:%d,fps:%d",x,y,GetFPS());
        ImGui::End();
    }
    rlImGuiEnd();
    EndDrawing();
}

static void InitLevel1(){
    // FEXDraw();
    Bimg = GenImageColor(4000, 4000, (Color){0,0,128,255});
    Btex = LoadTextureFromImage(Bimg);
}

static void UnInitLevel1(){
    ready = false;
    UnloadImage(Bimg);
}

inline void FEXDraw(){
    std::complex<float> comp(r,i);
    std::complex<float> ncomp(0.0,0.0);
    UnloadTexture(Btex);
    ImageClearBackground(&Bimg, (Color){0,0,128,255});
    in1 = 0;
    for (float ii = -2.00;ii<2.00;ii+=(4000.0/float(x)) * 0.001){
        in2=0;
        in1++;
        for (float jj = -2.00;jj<2.00;jj+=(4000.0/float(y)) * 0.001) {
            in2++;
            ncomp.imag(jj);
            ncomp.real(ii);
            fc[0] = ncomp;
            ok = true;
            for (int kk = 0;kk<n-1;kk++){
                fc[kk+1] = (std::complex<float>)(std::pow(fc[kk],nn)) + comp;
                if(std::abs(fc[kk+1])>2.0){
                    ok = false;
                    ImageDrawPixel(&Bimg, in1, in2, Jet2RGB(kk*(256/n)));
                    break;
                }
            }
            if (ok) {
                ImageDrawPixel(&Bimg, in1, in2, Jet2RGB(255));
            }
        }
    }
    Btex = LoadTextureFromImage(Bimg);
}
